package it.uniroma3.dia.grafica;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import winterwell.jtwitter.Twitter.User;
import it.uniroma3.dia.mysql.Tweet2db;
import it.uniroma3.dia.util.LoadProperties;

public class XML_creator {
	
	public  void createXMLGraphByItem(HashMap<Integer, LinkedList<User>> map, User rootUser){
		
		ShowGraph graph = new ShowGraph();
		String pathUtente = LoadProperties.getPathUtente().trim();
		/* CREAZIONE FILE XML */
		String head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!--  An excerpt of an egocentric social network  -->\n<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\">\n<graph edgedefault=\"undirected\">";
		String inizializzazione="<!-- data schema -->\n<key id=\"name\" for=\"node\" attr.name=\"name\" attr.type=\"string\"/>\n<key id=\"valutation\" for=\"node\" attr.name=\"valutation\" attr.type=\"string\"/>";
		String nodoPadre = "<node id=\"1\">\n<data key=\"name\">" + rootUser.getScreenName() + "</data>\n<data key=\"valutation\">item</data>\n</node>";
		String end ="</graph>\n</graphml>";
		
		List<User> utenti = new LinkedList<User>();
		Collection<LinkedList<User>> c  = map.values();
		Iterator<LinkedList<User>> it = c.iterator();

	    try {
	    	
	    	FileOutputStream file = new FileOutputStream(pathUtente);
	    	
	    	PrintStream Output = new PrintStream(file); 
	        Output.println(head);
	        Output.println(inizializzazione);
	        Output.println(nodoPadre);
	        
	        int contCluster = 2;
	        int contSon = map.size() + 2;
	        int lenght;
	        int cont;
	        int idCluster = 1;
	        String archi = "";

	        /* NODO PADRE */
	        while (it.hasNext()) {
	        	utenti = it.next();
	        	//Tweet2db.nuovoCluster(contCluster, utenti);
	        	Output.println("<node id=\"" + contCluster + "\">\n<data key=\"name\"> Cluster " + idCluster + "</data>\n<data key=\"valutation\">item</data>\n</node>");
	        	Output.println("<edge source=\"1\" target=\"" + contCluster + "\"></edge>\n");
	        	lenght = utenti.size();
	        	cont = 0;
	        	/* NODI FIGLI E ARCHI */
	        	while (lenght!=0) {
	        		Output.println("<node id=\"" + contSon + "\">\n<data key=\"name\">" + utenti.get(cont) + "</data>\n<data key=\"valutation\"/>\n</node>");
	        		archi = archi.concat("<edge source=\"" + contCluster + "\" target=\"" + contSon + "\"></edge>\n");
	        		contSon++;
	        		lenght--;
	        		cont++;
	        	}
	        	Output.println(archi);
	        	contCluster++;
	        	idCluster++;
	        }

	        Output.print(end);
	      }

	     catch (IOException e) {
	      System.out.println("Errore: " + e);
	      System.exit(1);
	    }
	
	     System.out.println("File XML creato Correttamente.");
	     graph.run();
	}

	public XML_creator() {
		// TODO Auto-generated constructor stub
	}

}